package com.ss.android.article.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.common.g.ImageMeasure;

public class ThumbGridLayout extends ViewGroup {
    public static final int DEFAULT_COLUMN = 3;
    public static final int SINGLE_IMAGE_UI_TYPE_NEW = 0;
    public static final int SINGLE_IMAGE_UI_TYPE_OLD = 1;
    public static final int SINGLE_IMAGE_UI_TYPE_WENDA = 2;

    public final class LayoutParams extends ViewGroup.LayoutParams {
        public int x;
        public int y;

        public LayoutParams(int arg1, int arg2) {
            super(arg1, arg2);
        }

        public LayoutParams(Context arg1, AttributeSet arg2) {
            super(arg1, arg2);
        }
    }

    private int vSpacing;
    private int hSpacing;
    private int column;
    private int singleWidth;
    private int singleHeight;
    private int itemHeight;
    private int itemWidth;

    public ThumbGridLayout(Context context) {
        this(context, null, 0);
    }

    public ThumbGridLayout(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        int v3 = 3;
        this.vSpacing = 0;
        this.hSpacing = 0;
        this.column = 0;
        this.singleWidth = 0;
        this.singleHeight = 0;
        this.itemHeight = 1;
        this.itemWidth = -1;
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, com.ss.android.article.base.a.ThumbGridLayout, defaultStyle, 0);
        this.vSpacing = typedArray.getDimensionPixelOffset(com.ss.android.article.base.a.ThumbGridLayout_thumbVSpacing, 0);
        this.hSpacing = typedArray.getDimensionPixelOffset(com.ss.android.article.base.a.ThumbGridLayout_thumbHSpacing, 0);
        this.column = typedArray.getInt(com.ss.android.article.base.a.W, v3);
        typedArray.recycle();
        if (this.vSpacing < 0) {
            this.vSpacing = 0;
        }

        if (this.hSpacing < 0) {
            this.hSpacing = 0;
        }

        if (this.column < 0) {
            this.column = v3;
        }
    }

    public ThumbGridLayout(Context arg2, AttributeSet arg3) {
        this(arg2, arg3, 0);
    }

    public final void setSingleSize(int arg1, int arg2) {
        this.singleWidth = arg1;
        this.singleHeight = arg2;
    }

    protected boolean checkLayoutParams(ViewGroup.LayoutParams arg2) {
        return arg2 instanceof LayoutParams;
    }

    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(-2, -2);
    }

    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet arg3) {
        return new LayoutParams(this.getContext(), arg3);
    }

    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams arg4) {
        return new LayoutParams(arg4.width, arg4.height);
    }

    public int getChildWidth() {
        return (UIUtils.getScreenWidth(this.getContext().getApplicationContext()) - (this.column - 1) * this.hSpacing) / this.column;
    }

    protected void onLayout(boolean b, int arg10, int arg11, int arg12, int arg13) {
        int v2 = this.getChildCount();
        int v1;
        for (v1 = 0; v1 < v2; ++v1) {
            View v3 = this.getChildAt(v1);
            ViewGroup.LayoutParams v0 = v3.getLayoutParams();
            v3.layout(((LayoutParams) v0).x,
                    ((LayoutParams) v0).y,
                    ((LayoutParams) v0).x + v3.getMeasuredWidth(),
                    ((LayoutParams) v0).y + v3.getMeasuredHeight());
        }
    }

    protected void onMeasure(int arg14, int arg15) {
        ViewGroup.LayoutParams v0_2;
        View v0_1;
        int v0 = 2;
        int v4 = 0;
        int v6 = MeasureSpec.EXACTLY;
        int v1 = View.MeasureSpec.getMode(arg14);
        int v5 = this.getPaddingTop();
        int v7 = this.getPaddingLeft();
        if (v1 == 0) {
            throw new IllegalArgumentException("Not support UNSPECIFIED mode in width!");
        }

        int v8 = this.getChildCount();
        int v9 = View.MeasureSpec.getSize(arg14);
        if (v8 != 1 || this.singleWidth <= 0 || this.singleHeight <= 0) {
            v1 = this.column;
            if (v8 == 4) {
                v1 = v0;
            }

            int v10 = (v9 - (this.column - 1) * this.hSpacing) / this.column;
            int v3 = View.MeasureSpec.makeMeasureSpec(v10, v6);
            int v2_1 = this.itemWidth > 0 ? View.MeasureSpec.makeMeasureSpec(this.itemWidth, v6) : v3;
            int v11 = View.MeasureSpec.getSize(v2_1);
            for (v0 = v5; v4 < v8; v0 = v6) {
                int v12 = v4 % v1 * (this.hSpacing + v10) + v7;
                v6 = v4 / v1 * (this.vSpacing + v11) + v5;
                v0_1 = this.getChildAt(v4);
                v0_1.measure(v3, v2_1);
                v0_2 = v0_1.getLayoutParams();
                ((LayoutParams) v0_2).x = v12;
                ((LayoutParams) v0_2).y = v6;
                ++v4;
            }

            v0 = v0 + v11 + this.getPaddingBottom();
        } else {
            ImageMeasure.Spec v2 = new ImageMeasure.Spec(this.singleWidth, this.singleHeight);
            if (this.itemHeight == v0) {
                ImageMeasure.updateSingleImageSpecForWD(v2, UIUtils.getScreenWidth(this.getContext()) - v9);
            } else if (this.itemHeight == 0) {
                ImageMeasure.updateSingleImageSpec2(v2, UIUtils.getScreenWidth(this.getContext()) - v9);
            } else {
                ImageMeasure.updateSingleImageSpec(v2);
            }

            v1 = v2.mHeight;
            v0_1 = this.getChildAt(0);
            v0_1.measure(View.MeasureSpec.makeMeasureSpec(v2.mWidth, v6), View.MeasureSpec.makeMeasureSpec(v2.mHeight, v6));
            v0_2 = v0_1.getLayoutParams();
            ((LayoutParams) v0_2).x = v7;
            ((LayoutParams) v0_2).y = v5;
            v0 = v1;
        }

        this.setMeasuredDimension(v9, v0);
    }

    public void setItemHeight(int arg1) {
        this.itemWidth = arg1;
    }

    public void setSingleImageUiType(int arg1) {
        this.itemHeight = arg1;
    }
}

